//
//  PicTableViewCell.m
//  MansApp
//
//  Created by lanou3g on 15/7/17.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "PicTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIImage+GIF.h"
@implementation PicTableViewCell

//@property (nonatomic,strong) UIView *backView;//大的背景
//@property (nonatomic,strong) UILabel *titleLabel;//名称
//@property (nonatomic,strong) UIImageView *contentImageView;//内容图片
//@property (nonatomic,strong) UIImageView *gondImageView;//点赞
//@property (nonatomic,strong) UIImageView *shareImageView;//分享


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //标题
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        self.titleLabel.frame = CGRectMake(kP6Height(20), kP6Height(10), kWidth - kP6Height(40), kP6Height(30));
        [self.contentView addSubview:_titleLabel];
        //内容(图片)
        self.contentImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kP6Height(20), self.titleLabel.frame.size.height + kP6Height(20),kWidth - kP6Height(40),kP6Height(100))];
        [self.contentView addSubview:_contentImageView];
        //赞
        self.goodButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.goodButton setBackgroundImage:[UIImage imageNamed:@"circle_icon_like@2x.png"] forState:UIControlStateNormal];
        [self.contentView addSubview:_goodButton];
        //分享
        self.shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.shareButton setBackgroundImage:[UIImage imageNamed:@"episode_icon_share@2x.png"] forState:UIControlStateNormal];
        [self.contentView addSubview:_shareButton];
        //中间分割线
        self.backView = [[UIView alloc]initWithFrame:CGRectZero];
        self.backView.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_backView];
        //底部分割线
        self.lineView = [[UIView alloc]initWithFrame:CGRectZero];
        self.lineView.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_lineView];
    }
    return self;
}
- (void)jsonWithUrl:(NSURL *)url
{
    [self.contentImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"5.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.picHeight = image.size.height;
        self.picWidth = image.size.width;
        float scale = self.picWidth / (kWidth - kP6Height(40));
        float height = self.picHeight / scale;
        
        self.contentImageView.frame = CGRectMake(kP6Height(20), self.titleLabel.frame.size.height + kP6Height(20),kWidth - kP6Height(40), height);
        
        
    }];

}
- (void)cellSetModel:(PicModel *)model
{
    CGRect bounds = [model.title boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName] context:nil];
    self.titleLabel.frame = CGRectMake(kP6Height(20), kP6Height(10), kWidth - kP6Height(40), bounds.size.height);
    self.titleLabel.text = model.title;
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:15];
    
    NSRange range = [model.thumbnail_pic rangeOfString:@"gif"];
    if (range.length != 0) {
        NSURL *gifUrl = [NSURL URLWithString:model.thumbnail_pic];
        dispatch_group_t group = dispatch_group_create();
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_group_async(group, queue, ^{
            [self jsonWithUrl:gifUrl];
//            self.contentImageView.image = [UIImage sd_animatedGIFWithData:[NSData dataWithContentsOfURL:gifUrl]];
        });
    }else
    {
        NSURL *url = [NSURL URLWithString:model.thumbnail_pic];
        [self jsonWithUrl:url];
    }
        self.goodButton.frame = CGRectMake(kP6Height(79), self.contentImageView.frame.origin.y + self.contentImageView.frame.size.height + kP6Height(7), kP6Height(20), kP6Height(20));
    
        self.shareButton.frame = CGRectMake((kWidth / 2) + kP6Height(79), self.contentImageView.frame.origin.y + self.contentImageView.frame.size.height + kP6Height(5), kP6Height(30), kP6Height(30));
        
        self.backView.frame = CGRectMake((kWidth / 2) - kP6Height(1), self.contentImageView.frame.origin.y + self.contentImageView.frame.size.height + kP6Height(5), kP6Height(0.5), kP6Height(25));
        self.lineView.frame = CGRectMake(kP6Height(20), self.backView.frame.origin.y + self.backView.frame.size.height + kP6Height(5), kWidth - kP6Height(40), kP6Height(0.5));
}

//动态返回高度
- (CGFloat)height:(PicModel *)model
{
   __block CGFloat aaa = 0;
    NSURL *url = [NSURL URLWithString:model.thumbnail_pic];
    [self.contentImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"5.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.picHeight = image.size.height;
        self.picWidth = image.size.width;
        
        float scale = self.picWidth / (kWidth - kP6Height(40));
        float height = self.picHeight / scale;
        aaa = height + kP6Height(30) + kP6Height(35);
    }];
    
    CGRect bounds = [model.title boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    return aaa + bounds.size.height;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
